#include "data_sqlite.h"
#include <qobject.h>
#include <qbytearray.h>
DataSqlite::DataSqlite():DataReader()
{
	m_db = QSqlDatabase::addDatabase("QSQLITE", "user data");

}

DataSqlite::~DataSqlite()
{	
	m_query->clear();
	//delete m_query;
}

void DataSqlite::initialize(std::string db)
{
	
	m_db.setDatabaseName(QString::fromLatin1(db));
	if (!m_db.open())
	{
		qDebug() << m_db.lastError().text();
	}
	m_query = new QSqlQuery(m_db);

}



void DataSqlite::listCollections(uint32_t userid, DataExchange::CollectionsResponse* d)
{
	m_query->prepare("SELECT id,code , name ,category FROM collections ");
	if (!m_query->exec())
	{
		qDebug() << "Can't Execute contractsList Sqlite Query !";
	}
	else
	{


		while (m_query->next())
		{
			auto r = d->add_collection();

			r->set_id(m_query->value("id").toInt());
			r->set_code(m_query->value("code").toByteArray().data());
			r->set_name(m_query->value("name").toByteArray());
			r->set_category(m_query->value("category").toInt());

		}
	}
}

void DataSqlite::listContracts(uint32_t userid, DataExchange::ContractsResponse* d)
{
	
	
	m_query->prepare("SELECT rowid,symbol , name ,slotcnt, decimal_point FROM symbols ");
	if (!m_query->exec())
	{
		qDebug() << "Can't Execute contractsList Sqlite Query !";
	}
	else
	{


		while (m_query->next())
		{
			auto contract = d->add_contract();
			auto msymbol = m_query->value("symbol").toByteArray();
			contract->set_id(m_query->value("rowid").toInt());
			contract->set_exchange(static_cast<DataExchange::Exchange>(*(uint8_t*)(msymbol.data()) - '0'));
			contract->set_symbol(msymbol.mid(1,6));
			contract->set_name(m_query->value("name").toByteArray());
			contract->set_slotcnt(m_query->value("slotcnt").toInt());
			contract->set_pointcnt(m_query->value("decimal_point").toInt());

		}
	}
	
	
}

void DataSqlite::listFactors(uint32_t userid, DataExchange::FactorsResponse* d)
{
	m_query->prepare("SELECT id , code , name ,category FROM factors");
	if (!m_query->exec())
	{
		qDebug() << "Can't Execute getGlobalFactors Sqlite Query !";
	}
	else
	{

		while (m_query->next())
		{

			auto f = d->add_factor();
			f->set_id(m_query->value(0).value<uint32_t>());
			f->set_category(m_query->value("category").toInt());
			f->set_name (m_query->value("name").toString().toUtf8());
			f->set_code( m_query->value("code").toString().toLatin1());
			

		}
	}
	
}

void DataSqlite::listStrategies(uint32_t userid, DataExchange::StrategiesResponse* d)
{
	m_query->prepare("SELECT id ,  name ,collection,conditions FROM strategies");
	if (!m_query->exec())
	{
		qDebug() << "Can't Execute getGlobalFactors Sqlite Query !";
	}
	else
	{

		while (m_query->next())
		{

			auto f = d->add_strategy();
			f->set_id(m_query->value(0).value<uint32_t>());
			f->set_collection_id(m_query->value("collection").toInt());
			f->set_name(m_query->value("name").toString().toUtf8());
			f->set_factor_conditions(m_query->value("conditons").toByteArray());


		}
	}
}

void DataSqlite::collectionDetail(uint32_t userid, uint32_t rowid, DataExchange::Collection* d)
{
	m_query->prepare("SELECT symbols,factors FROM collections where rowid= ? ");
	m_query->bindValue(0, rowid);
	if (!m_query->exec())
	{
		qDebug() << "Can't Execute Sqlite Query :" << m_query->lastError();
	}
	else
	{

		while (m_query->next())
		{
			d->set_id(rowid);
			d->set_contracts(m_query->value(0).toByteArray());
			auto factorsids = m_query->value(0).toByteArray();
			for(int i=0;i< factorsids.size()/4;++i)
			d->add_factor_id(*(uint32_t*)(factorsids.data()+i*4));
		}
	}
	m_query->clear();
}
